package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author chengyi
 * @date 2022/10/13 14:28
 */
@RestController //响应JSON数据
@RequestMapping("admin/coupon")
public class AdminCouponController {
    @Autowired
    AdminCouponService adminCouponService;

    /**
     * 查找优惠券
     * @param info
     * @param name
     * @param type
     * @param status
     * @return
     */
    //page=1&limit=20&sort=add_time&order=desc
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, String name, Short type,Short status){
        CommonData<MarketCoupon> data = adminCouponService.list(info,name,type,status);
        return BaseRespVo.ok(data,"成功");
    }

    /**
     * 添加优惠券
     * @param marketCoupon
     * @return
     */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketCoupon marketCoupon){
        if(marketCoupon.getMin().compareTo(BigDecimal.ZERO)>0&&marketCoupon.getDiscount().compareTo(BigDecimal.ZERO)>0){
            MarketCoupon data = adminCouponService.create(marketCoupon);
            return BaseRespVo.ok(data,"成功");
        }else {
            return new BaseRespVo(null,"最低消费或满减金额不能为负数",400);

        }

    }

    /**
     * 编辑优惠券
     * @param marketCoupon
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCoupon marketCoupon){
        if(marketCoupon.getMin().compareTo(BigDecimal.ZERO)>0&&marketCoupon.getDiscount().compareTo(BigDecimal.ZERO)>0){
            MarketCoupon data = adminCouponService.update(marketCoupon);
            return BaseRespVo.ok(data,"成功");
        }else {
            return new BaseRespVo(null,"最低消费或满减金额不能为负数",400);

        }

    }

    /**
     * 删除优惠券
     * @param marketCoupon
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon){
        adminCouponService.delete(marketCoupon);
        return BaseRespVo.ok("成功");
    }

    /**
     * 读取优惠券的详情
     * @param id
     * @return
     */
    @RequestMapping("read")
    public BaseRespVo read(Integer id){
        MarketCoupon data = adminCouponService.read(id);
        return BaseRespVo.ok(data,"成功");
    }


    /**
     * 查询指定用户的优惠券使用状态
     * @param info
     * @param couponId
     * @param userId
     * @param status
     * @return
     */
    //page=1&limit=20&couponId=3&userId=32&status=3&sort=add_time&order=desc
    @RequestMapping("listuser")
    public BaseRespVo listUser(BasePageInfo info,Integer couponId,Integer userId,Short status){
        CommonData<MarketCouponUser> data = adminCouponService.listuser(info,couponId,userId,status);
        return BaseRespVo.ok(data,"成功");
    }

}
